package com.timi_licai.timi_licai_account.dialog;

import android.app.Dialog;
import android.content.ContentValues;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.timi_licai.timi_licai_account.MyConstants;
import com.timi_licai.timi_licai_account.R;
import com.timi_licai.timi_licai_account.database.DatabaseUtils;
import com.timi_licai.timi_licai_account.listener.EditBudgeInterface;

/**
 * Created by LGH on 2015/3/17.
 */
public class EditMoneyDialog extends Dialog implements View.OnClickListener {

    private EditText editMoney;
    private EditBudgeInterface listener;

    public void setListener (EditBudgeInterface listener) {
        this.listener = listener;
    }

    public EditMoneyDialog (Context context) {
        super(context);
    }

    public EditMoneyDialog (Context context, int theme) {
        super(context, theme);
    }

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_edit_money);
        editMoney = (EditText) findViewById(R.id.dialog_edit_money_input);
        TextView txtCancel = (TextView) findViewById(R.id.dialog_edit_money_cancel);
        TextView txtEnsure = (TextView) findViewById(R.id.dialog_edit_money_ensure);
        txtCancel.setOnClickListener(this);
        txtEnsure.setOnClickListener(this);
    }

    @Override
    public void onClick (View v) {
        int id = v.getId();
        switch (id){
            case R.id.dialog_edit_money_cancel:
                this.cancel();
                break;
            case R.id.dialog_edit_money_ensure:
                String inputContent = editMoney.getText().toString().trim();
                if (inputContent.equals("")) {
                    inputContent = "0";
                    updateEditMoney(Integer.parseInt(inputContent));
                    if (listener != null) {
                        listener.editBudgeMoney(Integer.parseInt(inputContent));
                    }
                }else {
                    updateEditMoney(Integer.parseInt(inputContent));
                    if (listener != null) {
                        listener.editBudgeMoney(Integer.parseInt(inputContent));
                    }
                }

                this.dismiss();
                break;
        }


    }

    private void updateEditMoney (int money) {
        ContentValues values = new ContentValues();
        values.put("edit_money", money);
        DatabaseUtils.update(MyConstants.TABLE_EDIT_BUDGE, values,
                "_id=?", new String[] {"1"});
    }
}
